Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom paymaster handler & zyfi integration #51

Merged
merged 7 commits into from
Feb 6, 2025

Conversation

0xSulpiride
Copy link
Contributor

@0xSulpiride 0xSulpiride commented Jan 28, 2025

Description

Added paymaster plugin support that enables developers to create external paymaster plugins that integrate with SDK easily.

Key changes:

  • New CustomPaymasterHandler type and integration in transaction flow
  • Example implementation with ZyFi's paymaster API

feat: paymaster handler in session keys
feat: paymaster handler in passkeys
feat: general paymaster handler
feat: zyfi paymaster handler
wip: add paymaster in writeContract flow

Additional context

With these changes users can start using external paymasters easily with just 2-3 lines of code

import { zksyncSsoConnector, callPolicy } from "zksync-sso/connector";
import { zksyncSepoliaTestnet } from "viem/chains";
import { createConfig, connect } from "@wagmi/core";
import { erc20Abi } from "viem";
import { createZyfiPaymaster, createGeneralPaymaster } from "zksync-sso/paymaster";

const zksyncConnectorWithSession = zksyncSsoConnector({
  authServerUrl: "http://localhost:3002/confirm",
  session: {
    feeLimit: parseEther("0.1"),
    transfers: [
      {
        to: testTransferTarget,
        valueLimit: parseEther("1"),
      },
    ],
  },
  paymasterHandler: createGeneralPaymaster(PaymasterContract.address),
});
const zksyncConnector = zksyncSsoConnector({
  authServerUrl: "http://localhost:3002/confirm",
  paymasterHandler: createZyfiPaymaster({ apiKey: 'xxxxx-xxxxx-xxxx' }),
});
const wagmiConfig = createConfig({
  chains: [chain],
  connectors: [zksyncConnector],
  transports: {
    [chain.id]: http(),
  },
});

How it works:

customPaymasterHandler acts as a middleware which is triggered just before signing a transaction.
Simplest paymaster handler will just add static paymaster and paymasterInput to a transaction.

const zksyncConnector = zksyncSsoConnector({
  authServerUrl: "http://localhost:3002/confirm",
  paymasterHandler: async () => ({
    paymaster: PaymasterContract.address,
    paymasterInput: getGeneralPaymasterInput({ innerInput: "0x" }),
  }),
});

The more complex one, like ZyFi's, will send the transaction to some backend service and can fetch not only paymasterInput, but also gas fees since some paymasters may rely on exact values of these fields to account for ERC20 tokens.

@MexicanAce
Copy link
Contributor

Overall, the addition of paymasterHandler looks good to me 👍 . I might even consider adding an additional helper function to make general paymasters a one-line addition (for a future, separate PR):

BEFORE

const zksyncConnector = zksyncSsoConnector({
  authServerUrl: "http://localhost:3002/confirm",
  paymasterHandler: async () => ({
    paymaster: PaymasterContract.address,
    paymasterInput: getGeneralPaymasterInput({ innerInput: "0x" }),
  }),
});

AFTER

const zksyncConnector = zksyncSsoConnector({
  authServerUrl: "http://localhost:3002/confirm",
  paymasterHandler: createGeneralPaymaster(PaymasterContract.address),
});

@0xSulpiride 0xSulpiride marked this pull request as ready for review February 3, 2025 14:29
@0xSulpiride 0xSulpiride changed the title [Draft] custom paymaster handler & zyfi integration feat: custom paymaster handler & zyfi integration Feb 5, 2025
MexicanAce
MexicanAce previously approved these changes Feb 6, 2025
JackHamer09
JackHamer09 previously approved these changes Feb 6, 2025
@MexicanAce
Copy link
Contributor

@0xSulpiride Repos within matter-labs orgs have signed commits enforced. Please rebase the commits in your branch such that your commits are signed (GitHub instructions available here)

@0xSulpiride 0xSulpiride dismissed stale reviews from JackHamer09 and MexicanAce via 41439c3 February 6, 2025 16:13
@0xSulpiride
Copy link
Contributor Author

@MexicanAce thanks for instructions. rebased with signed commits

@0xSulpiride
Copy link
Contributor Author

@MexicanAce the PR is ready to be merged 🚀

@MexicanAce MexicanAce merged commit d1dedc8 into matter-labs:main Feb 6, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants